package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.ISignUpChargeDao;
import com.eling.elcms.travel.model.SignUpCharge;
import com.eling.elcms.travel.model.SignUpRefund;
import com.eling.elcms.travel.model.view.SignUpChargeSaveView;
import com.eling.elcms.travel.model.view.SignUpChargeView;
import com.eling.elcms.travel.model.view.SignUpFeeView;
import com.eling.elcms.travel.service.ISignUpChargeManager;
import com.eling.elcms.travel.service.ISignUpRefundManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SignUpChargeManagerImpl extends GenericManagerImpl<SignUpCharge, Long> implements ISignUpChargeManager {

	ISignUpChargeDao signUpChargeDao;
	@Autowired
	private ISignUpRefundManager signUpRefundManager;

	@Autowired
	public void setMakesignUpChargeDao(ISignUpChargeDao signUpChargeDao) {
		this.dao = signUpChargeDao;
		this.signUpChargeDao = signUpChargeDao;
	}

	@Override
	public List<SignUpChargeView> queryPlanItem(Long pkServicePoint, Long pkDestination, Date startDate, Date startDateEnd, String planStatus) {
		List<SignUpChargeView> returnList = signUpChargeDao.queryPlanItem(pkServicePoint, pkDestination, startDate, startDateEnd, planStatus);
		return returnList;
	}

	@Override
	public List<SignUpChargeView> queryPlanItemByPk(Long pkPlanItem, Long pkServicePoint) {
		List<SignUpChargeView> returnList = signUpChargeDao.queryPlanItemByPk(pkPlanItem, pkServicePoint);
		return returnList;
	}

	@Override
	public List<SignUpCharge> saveCharge(SignUpChargeSaveView sucsView) {
		List<SignUpCharge> sucList = sucsView.getSucList();
		return signUpChargeDao.save(sucList);
	}

	@Override
	public List<SignUpFeeView> queryPlan(SignUpCharge cond) {
		List<SignUpFeeView> viewList=new ArrayList<SignUpFeeView>();
		String fetch=PropertyUtils.getProperty(cond, "fetchProperties");
		PropertyUtils.setProperty(cond, "fetchProperties",null);
		List<SignUpCharge> list = query(cond);
		PropertyUtils.setProperty(cond, "fetchProperties",fetch);
		SignUpRefund refund=new SignUpRefund();
		refund.setSignUpCharge(cond);
		List<Long> refundList =signUpRefundManager.query(refund).stream().filter(a->a.getRefundDate()!=null).collect(Collectors.toSet()).stream().map(a->a.getSignUpCharge().getPkSignUpCharge()).collect(Collectors.toList());;
		for(SignUpCharge charge : list){
			if(charge.getSignUpRegister().getExitGroup() == null || charge.getSignUpRegister().getExitGroup() == false){
				SignUpFeeView view=new SignUpFeeView();
				view.setSignUpCharge(charge);
				if(refundList.contains(charge.getPkSignUpCharge())){
					view.setRefund(true);
				}
				viewList.add(view);
				continue;
			}else if(charge.getSignUpRegister().getExitGroup() && charge.getChargeDate() != null && charge.getPayable() !=null){
				//收完费退团
				SignUpFeeView view=new SignUpFeeView();
				view.setSignUpCharge(charge);
				if(refundList.contains(charge.getPkSignUpCharge())){
					view.setRefund(true);
				}
				viewList.add(view);
				continue;
			}
		}
		return viewList;
	}
	
}
